﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BaHuan.BusinessLayer;
using BaHuan.Common;
using BaHuan.Entities;

namespace BaHuan
{
    public partial class FrmBCDXNT : Form
    {
        private QuanLyKhoManagement quanlykhoMgt = new QuanLyKhoManagement();
        private HangHoaManagement hanghoaMgt = new HangHoaManagement();
        private ChiNhanhCongTyMgt chinhanhcongtyMgt = new ChiNhanhCongTyMgt();
        private KhoManagement khoMgt = new KhoManagement();
        private CTKhoManagement ctkhoMgt = new CTKhoManagement();

        public FrmBCDXNT()
        {
            InitializeComponent();
        }

        #region Get data
        public List<Kho> GetAllKho()
        {
            ResultObj result = new ResultObj();
            result = khoMgt.GetAllKho();
            List<Kho> lstKho = (List<Kho>)result.Result;
            return lstKho;
        }

        public List<CTKho> GetAllCTKho()
        {
            ResultObj result = new ResultObj();
            result = ctkhoMgt.GetAllCTKho();
            List<CTKho> lstCTKho = (List<CTKho>)result.Result;
            return lstCTKho;
        }

        public List<HangHoa> GetAllHangHoa()
        {
            ResultObj result = new ResultObj();
            result = hanghoaMgt.GetAllHangHoa();
            List<HangHoa> lstHangHoa = (List<HangHoa>)result.Result;
            return lstHangHoa;
        }

        public List<BangCanDoiXuatNhapTon> GetAllBCDXNT()
        {
            ResultObj result = new ResultObj();
            result = quanlykhoMgt.GetAllBCDXNT();
            List<BangCanDoiXuatNhapTon> lstBCDXNT = (List<BangCanDoiXuatNhapTon>)result.Result;
            return lstBCDXNT;
        }

        public List<ChiNhanhCongTy> GetAllChiNhanhCongTy()
        {
            ResultObj result = new ResultObj();
            result = chinhanhcongtyMgt.GetAllChiNhanhCongTy();
            List<ChiNhanhCongTy> lstchinhanhcty = (List<ChiNhanhCongTy>)result.Result;
            return lstchinhanhcty;
        }
        #endregion        

        #region Button and Event
        private void FrmBCDXNT_Load(object sender, EventArgs e)
        {           
            cboDoiTuong.DataSource = GetAllChiNhanhCongTy();
            cboDoiTuong.DisplayMember = "TenChiNhanh";
            cboDoiTuong.ValueMember = "Id";

            cboMaKho.DataSource = GetAllKho();
            cboMaKho.DisplayMember = "TenKho";
            cboMaKho.ValueMember = "Id";
        }

        private void cboDoiTuong_Leave(object sender, EventArgs e)
        {
            foreach (ChiNhanhCongTy b in cboDoiTuong.Items)
            {
                if (b.TenChiNhanh.ToLower().Contains(cboDoiTuong.Text) && cboDoiTuong.Text!="")
                {
                    cboDoiTuong.SelectedValue = b.Id;
                    break;
                }
            }

            if (cboDoiTuong.SelectedValue == null)
            {
                cboDoiTuong.Text = "";
                cboDoiTuong.Focus();
            }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnDongY_Click(object sender, EventArgs e)
        {
            if (cboMaKho.Text == "")
            {
                MessageBox.Show("Không có dữ liệu phù hợp!", "Thông báo");
                return;
            }

            List<BangCanDoiXuatNhapTon> lstBCDXNT = new List<BangCanDoiXuatNhapTon>();

            DateTime dtTuNgay = dtpTuNgay.Value;
            DateTime dtDenNgay = dtpDenNgay.Value;

            int sMaKho = ((Kho)cboMaKho.SelectedItem).Id;
            int iIdChiNhanhCongTy = 0;
            string sTenChiNhanh = "";

            if (cboDoiTuong.SelectedItem != null)
            {
                ChiNhanhCongTy chinhanhcongty = (ChiNhanhCongTy)cboDoiTuong.SelectedItem;
                iIdChiNhanhCongTy = chinhanhcongty.Id;
                sTenChiNhanh = chinhanhcongty.TenChiNhanh;
            }

            var lst = (from p in GetAllCTKho()
                       where p.IdKho == sMaKho
                       select p);

            if (lst.ToList().Count > 0)
            {
                foreach (var l in lst)
                {
                    long lSLTonDau = 0;
                    long lGTTonDau = 0;
                    long lSLNhap = 0;
                    long lGTNhap = 0;
                    long lSLXuat = 0;
                    long lGTXuat = 0;
                    long lSLTonCuoi = 0;
                    long lGTTonCuoi = 0;

                    lSLTonDau = l.SoLuongBanDau;
                    lGTTonDau = l.GiaTriBanDau;

                    HangHoa hanghoa = (from p in GetAllHangHoa()
                                       where p.Id == l.IdHangHoa
                                       select p).FirstOrDefault();

                    var lstPhatSinh = (from t in GetAllBCDXNT()
                                       where t.NgayThang.Date >= dtpTuNgay.Value.Date && t.NgayThang.Date <= dtpDenNgay.Value.Date
                                            && t.mavattu == hanghoa.MaSo && t.IdChiNhanh == iIdChiNhanhCongTy
                                       select t);

                    var lstTonDau = (from t in GetAllBCDXNT()
                                     where t.NgayThang.Date < dtTuNgay.Date && t.mavattu == hanghoa.MaSo && t.IdChiNhanh == iIdChiNhanhCongTy
                                     select t);

                    if (lstTonDau.ToList().Count > 0)
                    {
                        foreach (var r in lstTonDau)
                        {
                            lSLTonDau = (lSLTonDau + r.sln) - r.slx;
                            lGTTonDau = (lGTTonDau + r.gtn) - r.gtx;
                        }
                    }

                    if (lstPhatSinh.ToList().Count > 0)
                    {
                        foreach (var r in lstPhatSinh)
                        {
                            lSLNhap = lSLNhap + r.sln;
                            lGTNhap = lGTNhap + r.gtn;

                            lSLXuat = lSLXuat + r.slx;
                            lGTXuat = lGTXuat + r.gtx;
                        }
                    }

                    lSLTonCuoi = (lSLTonDau + lSLNhap) - lSLXuat;
                    lGTTonCuoi = (lGTTonDau + lGTNhap) - lGTXuat;

                    BangCanDoiXuatNhapTon b = new BangCanDoiXuatNhapTon();
                    b.mavattu = hanghoa.MaSo;
                    b.tenvattu = hanghoa.Ten;
                    b.dvt = hanghoa.DonViTinh;
                    b.sldk = lSLTonDau;
                    b.gtdk = lGTTonDau;
                    b.sln = lSLNhap;
                    b.gtn = lGTNhap;
                    b.slx = lSLXuat;
                    b.gtx = lGTXuat;
                    b.slck = lSLTonCuoi;
                    b.gtck = lGTTonCuoi;

                    lstBCDXNT.Add(b);
                }

                FrmXemReport frmXemReport = new FrmXemReport(lstBCDXNT, dtTuNgay, dtDenNgay, sTenChiNhanh.ToUpper());
                this.Hide();
                frmXemReport.ShowDialog();
                this.Show();
            }
            else
                MessageBox.Show("Không có dữ liệu phù hợp!", "Thông báo");
        }
        #endregion
    }
}
